<?php

namespace app\controller;

use app\Wechat;
use think\response\Json;

class Weixin
{
    public function jssdk(): Json
    {
        $data = new Wechat("wxc7da9a4cff406d19", "65db7e2491e16377dd76ce5c91af21f3");
        $signPackage = $data->getSignPackage();
        return json($signPackage);
    }

    public function openid()
    {
        $param = request()->param();
        if (empty($param['code'])) {
            $uri = urlencode("https://pandalife.store/get_openid/$param[baseUri]/$param[type]");
            header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7da9a4cff406d19&redirect_uri=$uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
            return;
        }
        //获取access_token
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxc7da9a4cff406d19&secret=65db7e2491e16377dd76ce5c91af21f3&code=$param[code]&grant_type=authorization_code";
        $res = json_decode(file_get_contents($url), true);
        if (empty($res['openid'])) {
            return json(['code' => 0, 'msg' => '获取openid失败']);
        }
//        header("location:http://padndalife.store/$param[baseUri]?openid=$res[openid]");
        $url = $param['type'] === 'local' ? "http://localhost:5173/" : "https://shop-t.pandalife.store/h5/";
        $url .= match ($param['baseUri']) {
            "recharge" => "pages/users/score/recharge?openid=" . $res['openid'],
            "tuan" => "pages/tuan/index?openid=" . $res['openid'],
            "cdd" => "pages/cdd/index?openid=" . $res['openid'],
            "membership" => "pages/users/vip/index?openid=" . $res['openid'],
            "gas" => "pages/chinaGas/index?openid=" . $res['openid'],
            default => "pages/index/index?openid=" . $res['openid'],
        };
        header("location:$url");
    }

    public function gasOpenid()
    {
        $param = request()->param();
        if (empty($param['code'])) {
            $uri = urlencode("http://pandalife.store/gas_openid/$param[gas]");
            header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7da9a4cff406d19&redirect_uri=$uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
            return;
        }

        //获取access_token
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxc7da9a4cff406d19&secret=65db7e2491e16377dd76ce5c91af21f3&code=$param[code]&grant_type=authorization_code";
        $res = json_decode(file_get_contents($url), true);
        if (empty($res['openid'])) {
            return json(['code' => 0, 'msg' => '获取openid失败']);
        }

        header("location:https://shop-t.pandalife.store/station_gas/?id=$param[gas]&openid=$res[openid]");
    }

    public function gasStationOpenid()
    {
        $param = request()->param();
        if (empty($param['code'])) {
            $uri = urlencode("http://pandalife.store/gas_station_openid/$param[supplier]");
            header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7da9a4cff406d19&redirect_uri=$uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
            return;
        }

        //获取access_token
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxc7da9a4cff406d19&secret=65db7e2491e16377dd76ce5c91af21f3&code=$param[code]&grant_type=authorization_code";
        $res = json_decode(file_get_contents($url), true);
        if (empty($res['openid'])) {
            return json(['code' => 0, 'msg' => '获取openid失败']);
        }

        header("location:https://shop-t.pandalife.store/h5/pages/gas_station/index?code=$param[supplier]&openid=$res[openid]");
    }
}
